package br.ufmg.dcc.vod.remoteworkers.async;

import java.rmi.RemoteException;

import br.ufmg.dcc.vod.remoteworkers.CommandError;

public abstract class AbstractQueueBasedCommandInterested<T, I> extends AbstractQueueBasedAsyncEntity<Response<T, I>> implements CommandInterested<T, I> {

	private static final long serialVersionUID = 1L;

	public AbstractQueueBasedCommandInterested(int port) throws RemoteException {
		super(port);
	}

	public final void commandResponse(I i, T t) throws RemoteException {
		queue(new Response<T, I>(false, i, t, null));
	}
	
	public final void commandError(I i, CommandError e) throws RemoteException {
		queue(new Response<T, I>(true, i, null, e));
	}
	
	@Override
	public final void receive(Response<T, I> response) {
		if (response.isError()) {
			realCommandError(response.getId(), response.getError());
		} else {
			realCommandResponse(response.getId(), response.getResponse());
		}
	}

	public abstract void realCommandError(I id, CommandError error);

	public abstract void realCommandResponse(I id, T response);
}